package com.effective;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RunTests {
	public static void main(String[] args) throws Exception {
		int tests = 0;
		int passed = 0;
		Class<?> testClass = Class.forName("com.effective.Sample2");
		for (Method m : testClass.getDeclaredMethods()) {
			if (m.isAnnotationPresent(ExceptionTest.class)) {
				tests++;
				try {
					m.invoke(null);
					System.out.printf("Test %s failed: no exception%n", m);
				} catch (InvocationTargetException wrappedEx) {
					Throwable exc = wrappedEx.getCause();
					Class<? extends Exception>[] excTypes = m.getAnnotation(ExceptionTest.class).value();
					for(Class<? extends Exception> exctype : excTypes) {
						if (exctype.isInstance(exc)) {
							passed++;
							System.out.printf("Test %s success: point exception%n", exc);
							break;
						}
					}
					
				} catch (Exception exc) {
					System.out.println("INVALID @Test: " + m);
				}
			}
		}
		System.out.printf("Passed: %d, Failed: %d%n", passed, tests - passed);
	}
}/**
* Output
Test java.lang.ArithmeticException: / by zero success: point exception
Test public static void com.effective.Sample2.m3() failed: no exception
Test java.lang.IndexOutOfBoundsException: Index: 5, Size: 0 success: point exception
Passed: 2, Failed: 2
* */
